<?php
require_once 'model.php';

/**
 * venue_model Class
 * Model for the venue table
 * 
 * @author Eduardo Nava
 *
 */
class venue_model extends model{
	/**
	 * Constructor
	 * @return void
	 */
	function __construct(){
		parent::__construct('venues');
	}

	/* (non-PHPdoc)
	 * @see app/model::get()
	 */
	function get($id){
		$venue = parent::get($id);
		return $this->addRelatedContent($venue);
	}

	/* (non-PHPdoc)
	 * @see app/model::getAll()
	 */
	function getAll($args=null){
		$venueList = array();
		foreach(parent::getAll($args) as $venue){
			$venueList[] = $this->addRelatedContent($venue);
		}
		return $venueList;
	}

	/**
	 * Function to Retrieve the I nformation Related to a Venue (address, ratings, etc.)
	 * @param $venue An associative array with the venue info 
	 * @return The Associative array with all the information required to be displayed
	 */
	private function addRelatedContent(&$venue){
		$venue['suburb'] = melbourne::getSuburb($venue['suburbs_id']);
		$venue['postcode'] = melbourne::getPostcode($venue['postcodes_id']);
		$venue['rating'] = $this->getRating($venue['id']);
		unset($venue['suburbs_id']);
		unset($venue['postcodes_id']);
		return $venue;
	}

	/**
	 * Function To retrieve The Venue Rating
	 * @param  $venueId The Id of the venue
	 * @return number
	 */
	function getRating($venueId){
		$sql = "SELECT avg( rate_level_id ) FROM `venue_rates` WHERE venue_id=$venueId";

		try{
			$rate = $this->dbCon->query($sql)->fetch(PDO::FETCH_NUM);
			return round($rate[0]);
		}catch (PDOException $e){
			//TODO find proper way to display message?
			echo $e->getMessage();
		}
	}

	/**
	 * Returns the number of rating that a venue has received
	 * @param $venueId The ID of the Venue to be looked up
	 * @return The number of rates
	 */
	function getRateCount($venueId){
		$sql = "SELECT count(*) FROM `venue_rates` WHERE venue_id=$venueId";

		try{
			$count = $this->dbCon->query($sql)->fetch(PDO::FETCH_NUM);
			return round($count[0]);
		}catch (PDOException $e){
			//TODO find proper way to display message?
			echo $e->getMessage();
		}
	}

	//TODO refactor this find is almost identical to beer->find
	/**
	 * Function to Perform venue Searches
	 * @param $args Taken from HTTP Request (venue_name,Suburb_name, etc)
	 * @return an Associative Array with info about The found Venues 
	 */
	function find($args=null){

		$venueName = isset($args['venue_name']) ? $args['venue_name'] : '';
		$suburbName = isset($args['suburb_name']) ? $args['suburb_name'] : '';

		$sql = "SELECT venues.id
				FROM venues LEFT JOIN suburbs ON (venues.suburbs_id = suburbs.id)
				WHERE venues.name LIKE '%$venueName%' and suburbs.name LIKE '%$suburbName%' ORDER BY venues.name";

		if(isset($args['page'])){
			$page = (int)$args['page'];
			$from = $page * $this->pageSize;
			$sql .= " LIMIT $from, $this->pageSize";
		}

		try{
			$venueTuples = $this->dbCon->query($sql)->fetchAll(PDO::FETCH_ASSOC);
		}catch (PDOException $e){
			//TODO find proper way to display message?
			echo $e->getMessage();
		}
		
		if(!isset($venueTuples)){
			return array();
		}
		
		$venueList = array();

		foreach($venueTuples as $venue){
			$venueList[] = $this->get($venue['id']);
		}
		return $venueList;
	}
}
?>